package Sequence;

public class Test0617 {
}

class Solution0617_1 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) return root2;
        if (root2 == null) return root1;

        merge(root1, root2);
        return root1;
    }

    public void merge(TreeNode root1, TreeNode root2) {
        TreeNode left1 = root1.left, left2 = root2.left;
        if (left1 != null&&left2!=null) {
            mergeTrees(left1, left2);
        } else if (left1 == null){
            root1.left = left2;
        }

        TreeNode right1 = root1.right, right2 = root2.right;
        if (right2 != null&&right1!=null) {
            mergeTrees(right1, right2);
        } else if (right1 == null) {
            root1.right = right2;
        }

        root1.val=root1.val+root2.val;
    }
}
